import {
  getReturnFactoryCustomFiledDetail,
  getReturnFactoryInspectCustomField,
  returnFactoryCustomFiledsDel,
  returnFactoryInspectCustomFieldAddOrUpdate,
  returnFactoryInspectCustomFIeldsMobileShow,
  returnFactoryInspectCustomFieldsRequired,
  returnFactoryInspectCustomFieldsUpdateStatus,
} from '@/api/return-factory'
import { ref } from 'vue'

export const useInspectCustomFieldForm = () => {
  const loading = ref(false)
  const form = ref<ReturnFactoryAPIRequest.InspectCustomField>({
    delFieldContentIds: [],
    fieldContentVos: [],
    id: '',
    mobileShow: true,
    name: '',
    required: true,
    status: false,
    tip: '',
    type: 1,
    uploadQty: 10,
    uploadSize: 100,
    delRepairModuleFieldRelationIds: [],
    repairModuleFieldRelationVos: [],
  })

  const resetForm = () => {
    form.value = {
      delFieldContentIds: [],
      fieldContentVos: [],
      id: '',
      mobileShow: true,
      name: '',
      required: true,
      status: false,
      tip: '',
      type: 1,
      uploadQty: 10,
      uploadSize: 100,
      delRepairModuleFieldRelationIds: [],
      repairModuleFieldRelationVos: [],
    }
  }

  const submit = async () => {
    loading.value = true
    const res = await returnFactoryInspectCustomFieldAddOrUpdate(form.value)
    loading.value = false
    return res.data
  }

  return {
    form,
    loading,
    submit,
    resetForm,
  }
}

export const useGetInspectCustomFields = () => {
  const loading = ref(false)
  const getListParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })
  const total = ref(0)
  const list = ref<Array<ReturnFactoryInspectCustomField>>([])

  const getList = async () => {
    loading.value = true
    const res = await getReturnFactoryInspectCustomField(getListParam.value)
    loading.value = false
    list.value = res.data.data.rows
    total.value = Number(res.data.data.total)
  }

  return {
    list,
    total,
    loading,
    getListParam,
    getList,
  }
}

export const useGetInspectCustomFieldDetail = () => {
  const loading = ref(false)
  const fieldDetail = ref<ReturnFactoryInspectCustomFieldDetail>({
    delFieldContentIds: [],
    delRepairModuleFieldRelationIds: [],
    fieldContentVos: [],
    id: '',
    mobileShow: true,
    name: '',
    repairModuleFieldRelationVos: [],
    required: true,
    status: false,
    tip: '',
    type: 1,
    uploadQty: 10,
    uploadSize: 100,
  })

  const getDetail = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryCustomFiledDetail(id)
    loading.value = false
    fieldDetail.value = res.data.data.row
  }

  return {
    loading,
    fieldDetail,
    getDetail,
  }
}

export const useFieldsDel = () => {
  const loading = ref(false)

  const del = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryCustomFiledsDel(ids)
    loading.value = false
    return res.data
  }

  return {
    loading,
    del,
  }
}

export const useFieldsUpdateStatus = () => {
  const loading = ref(false)

  const enable = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFieldsUpdateStatus(ids, false)
    loading.value = false
    return res.data
  }

  const disable = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFieldsUpdateStatus(ids, true)
    loading.value = false
    return res.data
  }

  return {
    loading,
    enable,
    disable,
  }
}

export const useFieldsMobileShow = () => {
  const loading = ref(false)

  const show = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFIeldsMobileShow(ids, true)
    loading.value = false
    return res.data
  }

  const hide = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFIeldsMobileShow(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    show,
    hide,
  }
}

export const useFieldsRequired = () => {
  const loading = ref(false)

  const required = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFieldsRequired(ids, true)
    loading.value = false
    return res.data
  }

  const unRequired = async (ids: Array<string>) => {
    loading.value = true
    const res = await returnFactoryInspectCustomFieldsRequired(ids, false)
    loading.value = false
    return res.data
  }

  return {
    loading,
    required,
    unRequired,
  }
}
